package com.example.base.config;

import com.example.base.config.handler.GlobalInterceptor;
import com.example.base.config.jsonfilter.JsonFilterHttpMessageConverter;
import com.example.mananger.handler.ManangerInterceptor;
import com.example.mananger.handler.OpenInterceptor;
import com.example.mananger.handler.SecurityInterceptor;
import com.example.mananger.handler.SystemInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.List;

@Slf4j
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {

    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        configurer.addPathPrefix("/api/v1", clazz -> clazz.getPackage().getName().startsWith("com.example.base.security.controller"));
        configurer.addPathPrefix("/api/v2",clazz -> clazz.getPackage().getName().startsWith("com.example.base.system.controller"));
        configurer.addPathPrefix("/api/open", clazz -> clazz.getPackage().getName().startsWith("com.example.open"));
        configurer.addPathPrefix("/api/flow",clazz->clazz.getPackage().getName().startsWith("com.example.base.flow"));
    }

    @Autowired
    private ConfigService configService;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        SpringConfig.setResourceHandlerRegistry(registry);
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/static/");

        String path=configService.getSysConfigByKey("resource");
        if(StringUtils.isNotBlank(path)){
            path="file:"+path;
            registry.addResourceHandler("/r/**")
                    .addResourceLocations(path);
        }

    }

    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new GlobalInterceptor())
                .addPathPatterns("/**"); //全局拦截

        registry.addInterceptor(new ManangerInterceptor())
                .addPathPatterns("/app/**"); //app 应用拦截

        registry.addInterceptor(new SecurityInterceptor())
                .addPathPatterns("/api/v1/**"); //系统安全拦截

        registry.addInterceptor(new SystemInterceptor())
                .addPathPatterns("/api/v2/**"); //系统信息拦截

        registry.addInterceptor(new OpenInterceptor())
                .addPathPatterns("/api/open/**"); //开放平台用户拦截

    }

    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.clear();
        JsonFilterHttpMessageConverter jsonFilterHttpMessageConverter = new JsonFilterHttpMessageConverter();
        converters.add(jsonFilterHttpMessageConverter);
    }

}
